package cc.eddic.pratice.programmingparadigms.emp02list.impl;

import cc.eddic.pratice.programmingparadigms.Result;
import cc.eddic.pratice.programmingparadigms.emp02list.ResultList;


public class ResultListHrjImpl implements ResultList {


    private Result[] results;

    public ResultListHrjImpl(int capacity) {
        results = new Result[capacity];
    }

    @Override
    public void add(Result result) {
        Result[] r = new Result[1];
        r[0] = result;

        if (results[results.length - 1] != null) {
            Result[] temp = new Result[results.length * 2];
            //使用System.arrycopy(原数组名,起始下标,新数组名,起始下标,复制长度)
            System.arraycopy(results, 0, temp, 0, results.length);
            results = temp;
        }

        for (int i = 0; i < results.length; i++) {
            if (results[i] == null) {
                results[i] = r[0];
                break;
            }
        }
    }

    @Override
    public Result get(int index) {
        if (index > size() - 1) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size());
        } else {
            return results[index];
        }
    }

    @Override
    public int size() {
        int i = 0;
        for (Result r : results) {
            if (r != null) {
                i++;
            }
        }
        return i;
    }

    @Override
    public Result findByGender(String gender) {

        for (int i = 0; i < size(); i++) {
            if (results[i].getKey().equals(gender)) {
                return results[i];
            }
        }
        return null;
    }

    @Override
    public Result findByDepartment(String department) {
        for (int i = 0; i < size(); i++) {
            if (results[i].getKey().equals(department)) {
                return results[i];
            }
        }
        return null;
    }

    @Override
    public Result findByJobTitle(String jobTitle) {
        for (int i = 0; i < size(); i++) {
            if (results[i].getKey().equals(jobTitle)) {
                return results[i];
            }
        }
        return null;
    }

}
